<template>
  <div style="width: 400px;margin: 300px auto">
    {{this.$store.getters.isLogin}}

    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="用户名" prop="userName">
        <el-input v-model.number="ruleForm.userName"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="userPwd">
        <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off"></el-input>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login",

  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
    };

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      }
    };

    return {
      ruleForm: {
        userName: '',
        userPwd: ''
      },
      rules: {
        userPwd: [
          {validator: validatePass, trigger: 'blur'}
        ],
        userName: [
          {validator: checkUsername, trigger: 'blur'}
        ]
      }
    };
  },

  methods: {
    async submitForm() {
      console.log(this.ruleForm.userName)

      let params = {
        userName: this.ruleForm.userName,
        userPwd: this.ruleForm.userPwd
      }
      console.log(params.userName)
      

      await this.$store.dispatch('login', params);

      if (this.$store.getters.getLoginUser != '') {

        if(this.$store.getters.getLoginUser.userType=='超级管理员'){
          this.$router.push('/nav')
        }
        if(this.$store.getters.getLoginUser.userType=='虫害管理员'){
          this.$router.push('/')
        }
        if(this.$store.getters.getLoginUser.userType=='资料管理员'){
          this.$router.push('/')
        }
       
        
      } else {
        
        alert("登录失败，密码或用户名错误")
      }

    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}





</script>

<style scoped>

</style>